const mysql = require('mysql')

// 创建一个连接对象
const con = mysql.createConnection({
    host: "localhost",
    port: "3306",
    user: "root",
    password: "root",
    database: "blog"
})
// 开启连接
con.connect()
// 发起数据库请求
function query(sql) {
    return new Promise((resolve, reject) => {
        return con.query(sql, (err, data) => {
            if (err) reject(err)
            resolve(data)
        })
    })
}
// 查询sql
let sql = `select * from blogs`;
// where 语法 查询 筛选
sql = `select * from blogs where title="少年"`
sql = `select * from blogs where title like "%少年%"` // 模糊匹配
sql = `select * from blogs where title like "%少年%" and id=2` // 并且
sql = `select * from blogs where title like "%少年%" or id=2` // 或者
// order 排序
sql = `select * from blogs where title like "%少年%" order by id desc` // 根据id倒叙
sql = `select * from blogs where title like "%少年%" order by create_time asc` // 根据时间正序
// limit 分页
sql = `select * from blogs where title like "%少年%" order by create_time asc limit 1,2` // 分页 从起始下标向后截取多少位
// where order limit 顺序不可改变

// 添加
let insertSql;
insertSql = `insert into blogs(title,content,author,create_time) values('哈哈哈','哈哈哈哈', 'kkk',${Date.now()})`
// 编辑
let updateSql = `update blogs set title='真菜',content="哈哈哈哈" where id=4`
// 删除
let deleteSql = `delete from blogs` // 删全表
deleteSql = `delete from blogs where id=4
`
query(deleteSql).then(res => {
    console.log(res)
})



// {
//     fieldCount: 0,
//     affectedRows: 1, // 受影响的行数
//     insertId: 4, // 新增数据的id 没有则为0
//     serverStatus: 2,
//     warningCount: 0,
//     message: '',
//     protocol41: true,
//     changedRows: 0
//   }








con.end()